const http = require('http');
var https = require('https');
var express = require('express');
var expressWs = require("express-ws");
var fs = require('fs');
var path = require('path');
var bodyParser = require('body-parser');
var mysql = require('mysql');
var app = express();
expressWs(app);
var dbm = mysql.createPool({
    host: '*',
    user: '*',
    password: '*',
    database: '*'
});


var urlencodedParser = bodyParser.urlencoded({limit: '50mb', extended: true})

var rdm = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
app.post('/接收体能数据接口', urlencodedParser, function (req, res) {
    console.log('\x1b[31m', new Date().toLocaleString(), '\x1b[39m');
    console.log("request from:" + req.ip + ",request path:" + req.url);
    var date = new Date();
    var y = date.getMonth() + 1;
    var csdf = req.body.csdf;
    var username = req.body.NameID;
    var TestTime = date.getFullYear() + '/' + y + '/' + date.getDate();
    var IndexSex = req.body.IndexSex;
    var Height = req.body.Height;
    var Age = req.body.Age;
    var csxcj = req.body.csxcj;
    var ckbz = req.body.ckbz;
    var ceshitixi = req.body.ceshitixi;
    var sixlevel = req.body.sixlevel;
    var xtjl = req.body.xtjl;
    var mjjl = req.body.mjjl;
    var nljl = req.body.nljl;
    var phjl = req.body.phjl;
    var rrjl = req.body.rrjl;
    var sdjl = req.body.sdjl;
    var lysjy = req.body.lysjy;
    var jiaoline = req.body.jiaoline;
    var wendinglineX = req.body.wendinglineX;
    var wendinglineY = req.body.wendinglineY;
    var phtips = req.body.phtips;
    var stdkjd = req.body.stdkjd;
    var szwqjd = req.body.szwqjd;
    var rrtips = req.body.rrtips;
    var sdline = req.body.sdline;
    var maxsd = req.body.maxsd;
    var sdtips = req.body.sdtips;
    var nlline = req.body.nlline;
    var slpxleft = req.body.slpxleft;
    var slpxright = req.body.slpxright;
    var slpxtips = req.body.slpxtips;
    var zdstjd = req.body.zdstjd;
    var pjstjd = req.body.pjstjd;
    var jfline = req.body.jfline;
    var mjtips = req.body.mjtips;
    var maxszfd = req.body.maxszfd;
    var zhongxintime = req.body.zhongxintime;
    var xttips = req.body.xttips;
    var qmszjy = req.body.qmszjy;
    var Stabilizationtime = req.body.Stabilizationtime;
    var tjxl = req.body.tjxl;
    var zi = req.body.zi;
    var datakey = "";
    var equipmentNo = req.body.equipmentNo;
    for (let i = 0; i < 18; i++) {
        datakey += rdm[Math.floor(Math.random() * 25)]
    }
    if (Height != null) {
        var sqlstr = 'insert into tn(csdf,username,TestTime,IndexSex,Height,Age,csxcj,ckbz,ceshitixi,sixlevel,xtjl,mjjl,nljl,phjl,rrjl,sdjl,lysjy,jiaoline,wendinglineX,wendinglineY,phtips,stdkjd,szwqjd,rrtips,sdline,maxsd,sdtips,nlline,slpxleft,slpxright,slpxtips,zdstjd,pjstjd,jfline,mjtips,maxszfd,zhongxintime,xttips,qmszjy,Stabilizationtime,tjxl,zi,datakey,equipmentNo)';
        sqlstr += 'values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);';
        dbm.query(sqlstr, [csdf, username, TestTime, IndexSex, Height, Age, csxcj, ckbz, ceshitixi, sixlevel, xtjl, mjjl, nljl, phjl, rrjl, sdjl, lysjy, jiaoline, wendinglineX, wendinglineY, phtips, stdkjd, szwqjd, rrtips, sdline, maxsd, sdtips, nlline, slpxleft, slpxright, slpxtips, zdstjd, pjstjd, jfline, mjtips, maxszfd, zhongxintime, xttips, qmszjy, Stabilizationtime, tjxl, zi, datakey, equipmentNo], function () {
            dbm.query('select MAX(id) from tn', function (err, data) {
                if (err) {
                    console.log('[SELECT ERROR] - ', err.message);
                    return;
                } else {
                    res.send('' + (data[0]['MAX(id)'] + 10000000));
                }
            })
        });
    } else {
        res.statusCode = 404;
        res.send('input is null');
    }
})
app.get('/获取体能数据接口', function (req, res) {
    console.log('\x1b[31m', new Date().toLocaleString(), '\x1b[39m');
    console.log("request from:" + req.ip + ",request path:" + req.url);
    var dataid = req.query.dataid;
    var datakey = req.query.key;
    if (dataid > 0) {
        dbm.query('select * from tn where (ID=' + dataid + ' AND datakey="' + datakey + '")', function (err, data) {
            if (err) {
                console.log('[SELECT ERROR] - ', err.message);
                return;
            } else {
                res.send(data[0]);
            }
        });
    } else {
        res.statusCode = 404;
        res.send('404');
    }
})

app.get('/获取体能历史数据接口', function (req, res) {
    console.log('\x1b[31m', new Date().toLocaleString(), '\x1b[39m');
    console.log("request from:" + req.ip + ",request path:" + req.url);
    var openid = req.query.openid;
    var num = req.query.num;
    if (openid != null && openid != "" && num != undefined && num != "" && num != null) {
        dbm.query('select id,username,csdf,TestTime,zi,datakey from tn where openid=' + openid + ' or openid2=' + openid + ' ORDER BY id DESC limit 10 offset ' + num, function (err, data) {
            if (err) {
                console.log('[SELECT ERROR] - ', err.message);
                return;
            } else {
                res.send(data);
            }
        });
    } else {
        res.statusCode = 404;
        res.send('404');
    }
})

app.post('/接收体成分数据', urlencodedParser, function (req, res) {
    console.log('\x1b[31m', new Date().toLocaleString(), '\x1b[39m');
    console.log("request from:" + req.ip + ",request path:" + req.url);

    var date = new Date();
    var y = date.getMonth() + 1;
    var NameID = req.body.NameID;
    var HealthScore = req.body.HealthScore;
    var TestTime = date.getFullYear() + '/' + y + '/' + date.getDate();
    var IndexSex = req.body.IndexSex;
    var Height = req.body.Height;
    var Age = req.body.Age;
    var Weight = req.body.Weight;
    var WTRangetop = req.body.WTRangetop;
    var WTRangebottom = req.body.WTRangebottom;
    var TBW = req.body.TBW;
    var TBWRangetop = req.body.TBWRangetop;
    var TBWRangebottom = req.body.TBWRangebottom;
    var PR = req.body.PR;
    var PRRangetop = req.body.PRRangetop;
    var PRRangebottom = req.body.PRRangebottom;
    var ISsalt = req.body.ISsalt;
    var ISRangetop = req.body.ISRangetop;
    var ISRangebottom = req.body.ISRangebottom;
    var FAT = req.body.FAT;
    var FATRangetop = req.body.FATRangetop;
    var FATRangebottom = req.body.FATRangebottom;
    var FFB = req.body.FFB;
    var FFBRangetop = req.body.FFBRangetop;
    var FFBRangebottom = req.body.FFBRangebottom;
    var SMM = req.body.SMM;
    var SMMRangetop = req.body.SMMRangetop;
    var SMMRangebottom = req.body.SMMRangebottom;
    var BMI = req.body.BMI;
    var BMIRangetop = req.body.BMIRangetop;
    var BMIRangebottom = req.body.BMIRangebottom;
    var PBF = req.body.PBF;
    var PBFRangetop = req.body.PBFRangetop;
    var PBFRangebottom = req.body.PBFRangebottom;
    var FATptbz = req.body.FATptbz;
    var wushikhz = req.body.wushikhz;
    var ebwushikhz = req.body.ebwushikhz;
    var jrjh = req.body.jrjh;
    var jrjhtop = req.body.jrjhtop;
    var jrjhbottom = req.body.jrjhbottom;
    var jdzf = req.body.jdzf;
    var jdzftop = req.body.jdzftop;
    var jdzfbottom = req.body.jdzfbottom;
    var zhjy = req.body.zhjy;
    var img1 = req.body.img1;
    var dqtx = req.body.dqtx;
    var yctx = req.body.yctx;
    var WeightControl = req.body.WeightControl;
    var MuscleControl = req.body.MuscleControl;
    var FatControl = req.body.FatControl;
    var kzjl = req.body.kzjl;

    var kzjy = req.body.kzjy;
    var BMR = req.body.BMR;
    var Obesity = req.body.Obesity;
    var WHR = req.body.WHR;
    var VFR = req.body.VFR;
    var txleve = req.body.txleve;
    var zi = req.body.zi;
    var datakey = "";
    var equipmentNo = req.body.equipmentNo;
    var time2 = Date.parse(date);
    for (let i = 0; i < 18; i++) {
        datakey += rdm[Math.floor(Math.random() * 25)]
    }
    if (Height != null) {
        var sqlstr = 'insert into tcf(HealthScore,NameID,TestTime,IndexSex,Height,Age,Weight,WTRangetop,WTRangebottom,TBW,TBWRangetop,TBWRangebottom,PR,PRRangetop,PRRangebottom,ISsalt,ISRangetop,ISRangebottom,FAT,FATRangetop,FATRangebottom,FFB,FFBRangetop,FFBRangebottom,SMM,SMMRangetop,SMMRangebottom,BMI,BMIRangetop,BMIRangebottom,PBF,PBFRangetop,PBFRangebottom,FATptbz,wushikhz,ebwushikhz,jrjh,jrjhtop,jrjhbottom,jdzf,jdzftop,jdzfbottom,zhjy,img1,dqtx,yctx,WeightControl,MuscleControl,FatControl,kzjl,kzjy,BMR,Obesity,WHR,VFR,txleve,,,zi,datakey,equipmentNo,time2)';
        sqlstr += 'values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);';
        dbm.query(sqlstr, [HealthScore, NameID, TestTime, IndexSex, Height, Age, Weight, WTRangetop, WTRangebottom, TBW, TBWRangetop, TBWRangebottom, PR, PRRangetop, PRRangebottom, ISsalt, ISRangetop, ISRangebottom, FAT, FATRangetop, FATRangebottom, FFB, FFBRangetop, FFBRangebottom, SMM, SMMRangetop, SMMRangebottom, BMI, BMIRangetop, BMIRangebottom, PBF, PBFRangetop, PBFRangebottom, FATptbz, wushikhz, ebwushikhz, jrjh, jrjhtop, jrjhbottom, jdzf, jdzftop, jdzfbottom, zhjy, img1, dqtx, yctx, WeightControl, MuscleControl, FatControl, kzjl, kzjy, BMR, Obesity, WHR, VFR, txleve, zi, datakey, equipmentNo, time2], function () {
            dbm.query('select MAX(id) from tcf', function (err, data) {
                if (err) {
                    console.log('[SELECT ERROR] - ', err.message);
                    return;
                } else {
                    res.send('' + (data[0]['MAX(id)'] + 30000000));
                }
            })
        });
    } else {
        res.statusCode = 404;
        res.send('input is null');
    }
})
app.get('/获取体成分数据', function (req, res) {
    console.log('\x1b[31m', new Date().toLocaleString(), '\x1b[39m');
    console.log("request from:" + req.ip + ",request path:" + req.url);
    var dataid = req.query.dataid;
    var datakey = req.query.key;
    if (dataid > 0) {
        dbm.query('select HealthScore,NameID,TestTime,IndexSex,Height,Age,Weight,WTRangetop,WTRangebottom,TBW,TBWRangetop,TBWRangebottom,PR,PRRangetop,PRRangebottom,ISsalt,ISRangetop,ISRangebottom,FAT,FATRangetop,FATRangebottom,FFB,FFBRangetop,FFBRangebottom,SMM,SMMRangetop,SMMRangebottom,BMI,BMIRangetop,BMIRangebottom,PBF,PBFRangetop,PBFRangebottom,FATptbz,wushikhz,ebwushikhz,jrjh,jrjhtop,jrjhbottom,jdzf,jdzftop,jdzfbottom,zhjy,dqtx,yctx,WeightControl,MuscleControl,FatControl,kzjl,kzjy,BMR,Obesity,WHR,VFR,txleve,equipmentNo from tcf where (ID=' + dataid + ' AND datakey="' + datakey + '")', function (err, data) {
            if (err) {
                console.log('[SELECT ERROR] - ', err.message);
                return;
            } else {
                res.send(data[0]);
            }
        });
    } else {
        res.statusCode = 404;
        res.send('404');
    }
})
app.get('/获取体成分b64图片', function (req, res) {
    console.log('\x1b[31m', new Date().toLocaleString(), '\x1b[39m');
    console.log("request from:" + req.ip + ",request path:" + req.url);
    var dataid = req.query.dataid;
    var datakey = req.query.key;
    if (dataid > 0) {
        dbm.query('select img1 from tcf where (ID=' + dataid + ' AND datakey="' + datakey + '")', function (err, data) {
            if (err) {
                console.log('[SELECT ERROR] - ', err.message);
                return;
            } else {
                res.send(data[0]);
            }
        });
    } else {
        res.statusCode = 404;
        res.send('404');
    }
})

app.get('/获取体成分历史数据', function (req, res) {
    console.log('\x1b[31m', new Date().toLocaleString(), '\x1b[39m');
    console.log("request from:" + req.ip + ",request path:" + req.url);
    var openid = req.query.openid;
    var num = req.query.num;
    if (openid != null && openid != "" && num != undefined && num != "" && num != null) {
        dbm.query('select id,NameID,HealthScore,TestTime,zi,datakey from tcf where openid=' + openid + ' or openid2=' + openid + ' ORDER BY id DESC limit 10 offset ' + num, function (err, data) {
            if (err) {
                console.log('[SELECT ERROR] - ', err.message);
                return;
            } else {
                res.send(data);
            }
        });
    } else {
        res.statusCode = 404;
        res.send('404');
    }
})
app.post('/接收体态数据', urlencodedParser, function (req, res) {
    console.log('\x1b[31m', new Date().toLocaleString(), '\x1b[39m');
    console.log("request from:" + req.ip + ",request path:" + req.url);
    var date = new Date();
    var y = date.getMonth() + 1;
    var NameID = req.body.NameID;
    var csdf = req.body.csdf;
    var TestTime = date.getFullYear() + '/' + y + '/' + date.getDate();
    var IndexSex = req.body.IndexSex;
    var Height = req.body.Height;
    var Age = req.body.Age;
    var img1 = req.body.img1;
    var img2 = req.body.img2;
    var img3 = req.body.img3;
    var fxlevel = req.body.fxlevel;
    var gupenbdfx = req.body.gupenbdfx;
    var gupenjl = req.body.gupenjl;
    var gjmslevel = req.body.gjmslevel;
    var zuzbUP = req.body.zuzbUP;
    var zuzbDown = req.body.zuzbDown;
    var zuzb = req.body.zuzb;
    var zujl = req.body.zujl;
    var gjsslevel = req.body.gjsslevel;
    var leftxiazhi = req.body.leftxiazhi;
    var rightxiazhi = req.body.rightxiazhi;
    var xiazhijl = req.body.xiazhijl;
    var nwblevel = req.body.nwblevel;
    var headwt = req.body.headwt;
    var headjy = req.body.headjy;
    var headwh = req.body.headwh;
    var gdjwt = req.body.gdjwt;
    var gdjjy = req.body.gdjjy;
    var gdjwh = req.body.gdjwh;
    var yjwt = req.body.yjwt;
    var yjjy = req.body.yjjy;
    var yjwh = req.body.yjwh;
    var jzcwwt = req.body.jzcwwt;
    var jzcwjy = req.body.jzcwjy;
    var jzcwwh = req.body.jzcwwh;
    var tbwt = req.body.tbwt;
    var tbjy = req.body.tbjy;
    var tbwh = req.body.tbwh;
    var gpwt = req.body.gpwt;
    var gpjy = req.body.gpjy;
    var gpwh = req.body.gpwh;
    var xnfwt = req.body.xnfwt;
    var xnfjy = req.body.xnfjy;
    var xnfwh = req.body.xnfwh;
    var xcswt = req.body.xcswt;
    var xcsjy = req.body.xcsjy;
    var xcswh = req.body.xcswh;
    var withSenor = req.body.withSenor;
    var zi = req.body.zi;
    var datakey = "";
    var equipmentNo = req.body.equipmentNo;
    for (let i = 0; i < 18; i++) {
        datakey += rdm[Math.floor(Math.random() * 25)]
    }
    if (Height != null) {
        var sqlstr = 'insert into tt(NameID,csdf,TestTime,IndexSex,Height,Age,img1,img2,img3,fxlevel,gupenbdfx,gupenjl,gjmslevel,zuzbUP,zuzbDown,zuzb,zujl,gjsslevel,leftxiazhi,rightxiazhi,xiazhijl,nwblevel,headwt,headjy,headwh,gdjwt,gdjjy,gdjwh,yjwt,yjjy,yjwh,jzcwwt,jzcwjy,jzcwwh,tbwt,tbjy,tbwh,gpwt,gpjy,gpwh,xnfwt,xnfjy,xnfwh,xcswt,xcsjy,xcswh,withSenor,zi,datakey,equipmentNo)';
        sqlstr += 'values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);'
        dbm.query(sqlstr, [NameID, csdf, TestTime, IndexSex, Height, Age, img1, img2, img3, fxlevel, gupenbdfx, gupenjl, gjmslevel, zuzbUP, zuzbDown, zuzb, zujl, gjsslevel, leftxiazhi, rightxiazhi, xiazhijl, nwblevel, headwt, headjy, headwh, gdjwt, gdjjy, gdjwh, yjwt, yjjy, yjwh, jzcwwt, jzcwjy, jzcwwh, tbwt, tbjy, tbwh, gpwt, gpjy, gpwh, xnfwt, xnfjy, xnfwh, xcswt, xcsjy, xcswh, withSenor, zi, datakey, equipmentNo], function () {
            dbm.query('select MAX(id) from tt', function (err, data) {
                if (err) {
                    console.log('[SELECT ERROR] - ', err.message);
                    return;
                } else {
                    res.send('' + (data[0]['MAX(id)'] + 20000000))
                }
            })
        });
    } else {
        res.statusCode = 404;
        res.send('input is null');
    }
})
app.get('/获取体态数据', function (req, res) {
    console.log('\x1b[31m', new Date().toLocaleString(), '\x1b[39m');
    console.log("request from:" + req.ip + ",request path:" + req.url);
    var dataid = req.query.dataid;
    var datakey = req.query.key;
    if (dataid > 0) {
        dbm.query('select NameID,csdf,TestTime,IndexSex,Height,Age,fxlevel,gupenbdfx,gupenjl,gjmslevel,zuzbUP,zuzbDown,zuzb,zujl,gjsslevel,leftxiazhi,rightxiazhi,xiazhijl,nwblevel,headwt,headjy,headwh,gdjwt,gdjjy,gdjwh,yjwt,yjjy,yjwh,jzcwwt,jzcwjy,jzcwwh,tbwt,tbjy,tbwh,gpwt,gpjy,gpwh,xnfwt,xnfjy,xnfwh,xcswt,xcsjy,xcswh,withSenor,equipmentNo FROM tt where (ID=' + dataid + ' AND datakey="' + datakey + '")', function (err, data) {
            if (err) {
                console.log('[SELECT ERROR] - ', err.message);
                return;
            } else {
                res.send(data[0]);
            }
        });
    } else {
        res.statusCode = 404;
        res.send('404');
    }
})
app.get('/获取体态b64图片', function (req, res) {
    console.log('\x1b[31m', new Date().toLocaleString(), '\x1b[39m');
    console.log("request from:" + req.ip + ",request path:" + req.url);
    var dataid = req.query.dataid;
    var datakey = req.query.key;
    if (dataid > 0) {
        dbm.query('select img1,img2,img3 from tt where (ID=' + dataid + ' AND datakey="' + datakey + '")', function (err, data) {
            if (err) {
                console.log('[SELECT ERROR] - ', err.message);
                return;
            } else {
                res.send(data[0]);
            }
        });
    } else {
        res.statusCode = 404;
        res.send('404');
    }
})
app.get('/获取体态历史数据', function (req, res) {
    console.log('\x1b[31m', new Date().toLocaleString(), '\x1b[39m');
    console.log("request from:" + req.ip + ",request path:" + req.url);
    var openid = req.query.openid;
    var num = req.query.num;
    if (openid != null && openid != "" && num != undefined && num != "" && num != null) {
        dbm.query('select id,NameID,csdf,TestTime,zi,datakey from tt where openid=' + openid + ' or openid2=' + openid + ' ORDER BY id DESC limit 10 offset ' + num, function (err, data) {
            if (err) {
                console.log('[SELECT ERROR] - ', err.message);
                return;
            } else {
                res.send(data);
            }
        });
    } else {
        res.statusCode = 404;
        res.send('404');
    }
})

app.post('/接收关节数据', urlencodedParser, function (req, res) {
    console.log('\x1b[31m', new Date().toLocaleString(), '\x1b[39m');
    console.log("request from:" + req.ip + ",request path:" + req.url);

    var date = new Date();
    var y = date.getMonth() + 1;
    var NameID = req.body.NameID;
    var TestScore = req.body.TestScore;
    var TestTime = date.getFullYear() + '/' + y + '/' + date.getDate();
    var IndexSex = req.body.IndexSex;
    var Height = req.body.Height;
    var Age = req.body.Age;
    var Joint = req.body.Joint;
    var Motion = req.body.Motion;
    var Action = req.body.Action;
    var MusclesDot = req.body.MusclesDot;
    var MusclesTips = req.body.MusclesTips;
    var MusclesSuggestionl = req.body.MusclesSuggestionl;
    var MusclesSuggestionr = req.body.MusclesSuggestionr;
    var NeckAngle = req.body.NeckAngle.replace(/[°]/g, "");
    var ShoulderAngle = req.body.ShoulderAngle.replace(/[°]/g, "");
    var SpineAngle = req.body.SpineAngle.replace(/[°]/g, "");
    var PelvisAngle = req.body.PelvisAngle.replace(/[°]/g, "");
    var KneeAngle = req.body.KneeAngle.replace(/[°]/g, "");
    var KneeAngle1 = req.body.KneeAngle1.replace(/[°]/g, "");
    var FootAngle = req.body.FootAngle.replace(/[°]/g, "");
    var FootAngle1 = req.body.FootAngle1.replace(/[°]/g, "");
    var NeckConclusion = req.body.NeckConclusion;
    var ShoulderConclusion = req.body.ShoulderConclusion;
    var SpineConclusion = req.body.SpineConclusion;
    var PelvisConclusion = req.body.PelvisConclusion;
    var KneeConclusion = req.body.KneeConclusion;
    var FootConclusion = req.body.FootConclusion;
    var NeckFlexiblel = req.body.NeckFlexiblel.replace(/[°]/g, "");
    var NeckFlexibler = req.body.NeckFlexibler.replace(/[°]/g, "");
    var TorsoFlexiblel = req.body.TorsoFlexiblel.replace(/[°]/g, "");
    var TorsoFlexibler = req.body.TorsoFlexibler.replace(/[°]/g, "");
    var UpperFlexiblel = req.body.UpperFlexiblel.replace(/[°]/g, "");
    var UpperFlexibler = req.body.UpperFlexibler.replace(/[°]/g, "");
    var PelvisFlexiblel = req.body.PelvisFlexiblel.replace(/[°]/g, "");
    var PelvisFlexibler = req.body.PelvisFlexibler.replace(/[°]/g, "");
    var LowerFlexiblel = req.body.LowerFlexiblel.replace(/[°]/g, "");
    var LowerFlexibler = req.body.LowerFlexibler.replace(/[°]/g, "");
    var JointTips = req.body.JointTips;
    var JointConclusion = req.body.JointConclusion;
    var Neckimg = req.body.Neckimg;
    var Spineimg = req.body.Spineimg;
    var Pelvisimg = req.body.Pelvisimg;
    var Kneeimg = req.body.Kneeimg;
    var Footimg = req.body.Footimg;
    var iknee = req.body.iknee;
    var ifoot = req.body.ifoot;
    var zi = req.body.zi;
    var equipmentNo = req.body.equipmentNo;
    var datakey = "";
    for (let i = 0; i < 18; i++) {
        datakey += rdm[Math.floor(Math.random() * 25)]
    }
    if (Height != null) {
        var sqlstr = 'insert into gjgn(NameID,TestScore,TestTime,IndexSex,Height,Age,Joint,Motion,Action,MusclesDot,MusclesTips,MusclesSuggestionl,MusclesSuggestionr,NeckAngle,ShoulderAngle,SpineAngle,PelvisAngle,KneeAngle,KneeAngle1,FootAngle,FootAngle1,NeckConclusion,ShoulderConclusion,SpineConclusion,PelvisConclusion,KneeConclusion,FootConclusion,NeckFlexiblel,NeckFlexibler,TorsoFlexiblel,TorsoFlexibler,UpperFlexiblel,UpperFlexibler,PelvisFlexiblel,PelvisFlexibler,LowerFlexiblel,LowerFlexibler,JointTips,JointConclusion,Neckimg,Spineimg,Pelvisimg,Kneeimg,Footimg,iknee,ifoot,zi,equipmentNo,datakey)';
        sqlstr += 'values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);'
        dbm.query(sqlstr, [NameID, TestScore, TestTime, IndexSex, Height, Age, Joint, Motion, Action, MusclesDot, MusclesTips, MusclesSuggestionl, MusclesSuggestionr, NeckAngle, ShoulderAngle, SpineAngle, PelvisAngle, KneeAngle, KneeAngle1, FootAngle, FootAngle1, NeckConclusion, ShoulderConclusion, SpineConclusion, PelvisConclusion, KneeConclusion, FootConclusion, NeckFlexiblel, NeckFlexibler, TorsoFlexiblel, TorsoFlexibler, UpperFlexiblel, UpperFlexibler, PelvisFlexiblel, PelvisFlexibler, LowerFlexiblel, LowerFlexibler, JointTips, JointConclusion, Neckimg, Spineimg, Pelvisimg, Kneeimg, Footimg, iknee, ifoot, zi, equipmentNo, datakey], function () {
            dbm.query('select MAX(id) from gjgn', function (err, data) {
                if (err) {
                    console.log('[SELECT ERROR] - ', err.message);
                    return;
                } else {
                    res.send('' + (data[0]['MAX(id)'] + 40000000));
                }
            })
        });
    } else {
        res.statusCode = 404;
        res.send('input is null');
    }
})

app.get('/获取关节数据', function (req, res) {
    console.log('\x1b[31m', new Date().toLocaleString(), '\x1b[39m');
    console.log("request from:" + req.ip + ",request path:" + req.url);
    var dataid = req.query.dataid;
    var datakey = req.query.key;
    if (dataid > 0) {
        dbm.query('select NameID,TestScore,TestTime,IndexSex,Height,Age,Joint,Motion,Action,MusclesDot,MusclesTips,MusclesSuggestionl,MusclesSuggestionr,NeckAngle,ShoulderAngle,SpineAngle,PelvisAngle,KneeAngle,KneeAngle1,FootAngle,FootAngle1,NeckConclusion,ShoulderConclusion,SpineConclusion,PelvisConclusion,KneeConclusion,FootConclusion,NeckFlexiblel,NeckFlexibler,TorsoFlexiblel,TorsoFlexibler,UpperFlexiblel,UpperFlexibler,PelvisFlexiblel,PelvisFlexibler,LowerFlexiblel,LowerFlexibler,JointTips,JointConclusion,iknee,ifoot,equipmentNo FROM gjgn where ID=' + dataid + ' AND datakey="' + datakey + '"', function (err, data) {
            if (err) {
                console.log('[SELECT ERROR] - ', err.message);
                return;
            } else {
                res.send(data[0]);
            }
        });
    } else {
        res.statusCode = 404;
        res.send('404');
    }
})
app.get('/获取关节b64图片', function (req, res) {
    console.log('\x1b[31m', new Date().toLocaleString(), '\x1b[39m');
    console.log("request from:" + req.ip + ",request path:" + req.url);
    var dataid = req.query.dataid;
    var datakey = req.query.key;
    if (dataid > 0) {
        dbm.query('select Neckimg,Spineimg,Pelvisimg,Kneeimg,Footimg from gjgn where ID=' + dataid + ' AND datakey="' + datakey + '"', function (err, data) {
            if (err) {
                console.log('[SELECT ERROR] - ', err.message);
                return;
            } else {
                res.send(data[0]);
            }
        });
    } else {
        res.statusCode = 404;
        res.send('404');
    }
})
app.get('/获取关节历史数据', function (req, res) {
    console.log('\x1b[31m', new Date().toLocaleString(), '\x1b[39m');
    console.log("request from:" + req.ip + ",request path:" + req.url);
    var openid = req.query.openid;
    var num = req.query.num;
    if (openid != null && openid != "" && num != undefined && num != "" && num != null) {
        dbm.query('select id,NameID,TestScore,TestTime,zi,datakey from gjgn where openid=' + openid + ' or openid2=' + openid + ' ORDER BY id DESC limit 10 offset ' + num, function (err, data) {
            if (err) {
                console.log('[SELECT ERROR] - ', err.message);
                return;
            } else {
                res.send(data);
            }
        });
    } else {
        res.statusCode = 404;
        res.send('404');
    }
})

// 路由请求超时的中间件
app.use('/mSetPostureData', function (req, res, next) {
    // 这里必须是Response响应的定时器【120秒】
    res.setTimeout(120 * 1000, function () {
        console.log("Request has timed out.");
        return res.status(408).send("请求超时")
    });
    next();
});

//拦截一切请求
app.use(express.static(path.join(__dirname, 'file')))
app.get('/*', function (req, res) {
    console.log('\x1b[31m', new Date().toLocaleString(), '\x1b[39m');
    console.log("request from:" + req.ip + ",request path:" + req.url);
    res.sendFile(__dirname + "/file" + req.url);
})

function errorHandler(err, req, res, next) {
    console.log('\x1b[31m', new Date().toLocaleString(), '\x1b[39m');
    console.error(err.stack);
    res.statusCode = 404;
    res.send('404');
}

app.use(errorHandler);
var serverHttp = http.createServer(app).listen(1005, function () {

    var host = serverHttp.address().address
    var port = serverHttp.address().port
    console.log("http://%s:%s", host, port)

})


